﻿@model List<IdentityUser>
@{
    ViewData["Title"] = "用户";
    int id = 0;
}

<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        用户信息
        <small></small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-map-marker"></i> 主页</a></li>
        <li><a href="#">用户管理</a></li>
        <li class="active">用户信息</li>
    </ol>
</section>
<!-- Main content -->
<section class="content">
    <!-- Default box -->
    <div class="box">
        <div class="box-header with-border">
            <h3 class="box-title">所有用户</h3>
            <div class="box-tools pull-right">
                <button type="button" class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip"
                        title="Collapse">
                    <i class="fa fa-minus"></i>
                </button>
                <button type="button" class="btn btn-box-tool" data-widget="remove" data-toggle="tooltip" title="Remove">
                    <i class="fa fa-times"></i>
                </button>
            </div>
        </div>
        <div class="box-body">
            <!-- Main content -->
            <section class="content">
                <div class="row">
                    <div class="col-md-12">
                        <div class="box box-primary">
                            <div class="box-body">
                                <div class="row">
                                    <div class="col-md-12">
                                        <table id="grid" name="grid" class="table table-striped table-bordered">
                                            <thead>
                                                <tr>
                                                    <th width="20%">名字</th>
                                                    <th width="20%">邮箱</th>
                                                    <th width="20%">锁定</th>
                                                    <th align="center"></th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                @foreach (var item in Model)
                                                {
                                                    id = id + 1;

                                                    <tr>

                                                        <th>@item.UserName</th>
                                                        <th>@item.Email</th>
                                                        <th>@(item.LockoutEnd.HasValue && item.LockoutEnd.Value > DateTime.UtcNow ? "是" : "否")</th>
                                                        <th align="center">

                                                            <a class="btn btn-warning btn-xs" style="margin-left:2px" onclick="ShowPopup('/User/ChangePassword?id=@item.Id')"><i class='fa fa-key'>修改密码</i></a>
                                                            <a class="btn btn-danger btn-xs" style="margin-left:2px" onclick="Delete('@item.Id')"><i class='fa fa-remove'>删除</i></a>
                                                        </th>
                                                    </tr>
                                                }
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>

            </section>
        </div>
        @if (User.IsInRole("Admin"))
        {
            <!-- /.box-body -->
            <div class="box-footer">
                <div class="col-lg-2">
                    <div class="row">
                        <div class="col-md-12">
                            <a class="btn btn-primary" style="margin-bottom:10px" onclick="ShowPopup('/User/Add')"><i class="fa fa-plus"></i> 新建用户</a>
                        </div>
                    </div>
                </div>

            </div>
        }
        <!-- /.box-footer-->
    </div>
    <!-- /.box -->
</section>
<!-- /.content -->
<!--modal placeholder-->
<div class="modal modal-primary fade" id="modalDefault">
    <div class="modal-dialog">
        <div class="modal-content">

        </div>
    </div>
</div>

@section Scripts{
    @await Html.PartialAsync("_ValidationScriptsPartial")

    <script>

        $(document).ready(function () {

            $('#grid').DataTable({
                "oLanguage": {
                    "sLengthMenu": "每页显示 _MENU_ 条记录",
                    "sZeroRecords": "对不起，没有匹配的数据",
                    "sInfo": "第 _START_ - _END_ 条 / 共 _TOTAL_ 条数据",
                    "sInfoEmpty": "没有匹配的数据",
                    "sInfoFiltered": "(数据表中共 _MAX_ 条记录)",
                    "sProcessing": "正在加载中...",
                    "sSearch": "全文搜索：",
                    "oPaginate": {
                        "sFirst": "第一页",
                        "sPrevious": " 上一页 ",
                        "sNext": " 下一页 ",
                        "sLast": " 最后一页 "
                    },
                },

                "lengthChange": false,
                "order": [[0, 'desc']],

            });
        });

        function ShowPopup(url) {
            var modalId = 'modalDefault';
            var modalPlaceholder = $('#' + modalId + ' .modal-dialog .modal-content');
            $.get(url)
                .done(function (response) {
                    modalPlaceholder.html(response);
                    popup = $('#' + modalId + '').modal({
                        keyboard: false,
                        backdrop: 'static'
                    });
                }).fail(function (XMLHttpRequest, textStatus, errorThrown) {
                    if (errorThrown === 'Unauthorized') {

                        ShowMessageError("Session 过期，请重新登录");

                        window.location.href = "/Account/Login";

                    } else {
                        ShowMessageError(XMLHttpRequest.responseText);

                    }
                });
        }


        function SubmitAddEdit(form) {
            $.validator.unobtrusive.parse(form);
            if ($(form).valid()) {
                var data = $(form).serializeJSON();
                data = JSON.stringify(data);
                $.ajax({
                    type: 'POST',
                    url: '/User/Save',
                    data: data,
                    contentType: 'application/json',
                    success: function (data) {
                        if (data.success) {
                            popup.modal('hide');

                            ShowMessage(data.message);
                            window.location.href = "/User/Index";


                        } else {

                            ShowMessageError(data.message);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        if (errorThrown === 'Unauthorized') {

                            ShowMessageError("Session 过期，请重新登录");

                            window.location.href = "/Account/Login";

                        } else {
                            ShowMessageError(XMLHttpRequest.responseText);

                        }

                    }
                });

            }
            return false;
        }

        function SubmitAdd(form) {
            $.validator.unobtrusive.parse(form);
            if ($(form).valid()) {
                var data = $(form).serializeJSON();
                data = JSON.stringify(data);
                $.ajax({
                    type: 'POST',
                    url: '/User/Add',
                    data: data,
                    contentType: 'application/json',
                    success: function (data) {
                        if (data.success) {
                            popup.modal('hide');
                            ShowMessage(data.message);
                            window.location.href = "/User/Index";
                        } else {
                            ShowMessageError(data.message);

                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        if (errorThrown === 'Unauthorized') {

                            ShowMessageError("Session 过期，请重新登录");

                            window.location.href = "/Account/Login";

                        } else {
                            ShowMessageError(XMLHttpRequest.responseText);

                        }

                    }
                });

            }
            return false;
        }
        function SubmitChangePassword(form) {
            $.validator.unobtrusive.parse(form);
            if ($(form).valid()) {
                var data = $(form).serializeJSON();
                data = JSON.stringify(data);
                $.ajax({
                    type: 'POST',
                    url: '/User/ChangePassword',
                    data: data,
                    contentType: 'application/json',
                    success: function (data) {
                        if (data.success) {
                            popup.modal('hide');
                            ShowMessage(data.message);
                            window.location.href = "/User/Index";

                        } else {
                            ShowMessageError(data.message);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        if (errorThrown === 'Unauthorized') {

                            ShowMessageError("Session 过期，请重新登录");

                            window.location.href = "/Account/Login";

                        } else {
                            ShowMessageError(XMLHttpRequest.responseText);

                        }

                    }
                });

            }
            return false;
        }
        function Delete(id) {
            swal({
                title: "确定删除?",
                text: "",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#dd4b39",
                confirmButtonText: "是的",
                cancelButtonText: "取消",
                closeOnConfirm: true
            }, function () {
                $.ajax({
                    type: 'POST',
                    url: '/User/Remove?id=' + id,
                    success: function (data) {
                        if (data.success) {
                            ShowMessage(data.message);
                            window.location.href = "/User/Index";
                        } else {
                            ShowMessageError(data.message);

                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        if (errorThrown === 'Unauthorized') {

                            ShowMessageError("Session 过期，请重新登录");

                            window.location.href = "/Account/Login";

                        } else {
                            ShowMessageError(XMLHttpRequest.responseText);

                        }

                    }
                });
            });


        }
    </script>
}